﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using Royalsoft.Common;
namespace NJL.WebManage.SystemAdmin
{
    public partial class EditAdmin : BasePage
    {
        NJL.BLL.NJL_SYS_ADMIN bll = new BLL.NJL_SYS_ADMIN();
        NJL.Model.NJL_SYS_ADMIN model;
        NJL.BLL.NJL_SYS_BASE bllSysBase = new BLL.NJL_SYS_BASE();
        NJL.BLL.NJL_SYS_CS_ADMIN_TYPE bllSysCsAdminType = new BLL.NJL_SYS_CS_ADMIN_TYPE();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                ddlArea.DataSource = bllSysBase.GetList("");
                ddlArea.DataTextField = "WEBSITE_NAME";
                ddlArea.DataValueField = "ID";
                ddlArea.DataBind();

                ddlUserType.DataSource = bllSysCsAdminType.GetList("");
                ddlUserType.DataTextField = "TYPE_NAME";
                ddlUserType.DataValueField = "TYPE_CODE";
                ddlUserType.DataBind();

                model = bll.GetModel(Request.QueryString["id"].ToString());
                if (model != null)
                {
                    txtUserName.Text = model.ADMIN_NAME;
                    txtPwd.Text = model.ADMIN_PASSWORD;
                    ddlArea.Items.FindByValue(model.NJL_SYS_BASE_ID).Selected = true;
                    ddlUserType.Items.FindByValue(model.TYPE_CODE).Selected = true;
                }
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            model = bll.GetModel(Request.QueryString["id"].ToString());
            if (model != null)
            {
                model.ADMIN_NAME = txtUserName.Text.Trim();
                if (txtPwd.Text != "")
                {
                    model.ADMIN_PASSWORD = txtPwd.Text.Trim();
                    model.ADMIN_PASSWORD_MD5 = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPwd.Text.Trim(), "MD5");
                }
                model.ID = Request.QueryString["id"].ToString();
                model.NJL_SYS_BASE_ID = ddlArea.SelectedValue;
                model.TYPE_CODE = ddlUserType.SelectedValue;
                model.UPDATE_DATE_TIME = System.DateTime.Now;

                if (bll.Update(model))
                {
                    MessageBox.ShowAndRedirect(this, "更新成功", "AdminManage.aspx");
                }
                else
                {
                    MessageBox.Show(this, "更新失败");
                }
            }
        }
    }
}